<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncGetCategoryPathList.php,v $
 * $Author: pcornelissen $
 * $Revision: 1.2 $
 * $Date: 2003/12/08 12:09:47 $
 *
 * Type: Function
 *
 * creates an array of categories \n
 * Example: If cid=1 is parent of cid=2:\n
 *          array[1]['Name']...\n
 *          array[2]['Name']...\n
 *          You can now foreach through this array and build a category tree\n
 *@param params['CategoryID']  int category id as starting point, if not set, we start at the top most catgory
 *@param params['assign']  string name of the assign that will hold the result array
 *@return array of array of category information     \n      
 *        'Name' categoy name               \n              
 *        'ID' category id                     \n           
 *        'parentID', parent category id        \n          
 *        'VATID' vatid                             \n      
 *         'LinkURL' link to categoryview to this category \n
  */
function smarty_function_pncGetCategoryPathList($params, &$smarty)
{
    extract($params);


    if( !$CategoryID )
    {
        $smarty->trigger_error( "pncGetCategoryPathList: missing parameter CategoryID" );
        return false;
    }
    
    if( !$assign )
    {
        $assign = "CategoryList";
    }

    if( pnModAPILoad('pncommerce','user') )
    {
        $catlist = array();
        $startcategory = $CategoryID;
        while( $CategoryID)
        {
            list( $CategoryName, $ParentID, $VATID ) = pnModAPIFunc ( 'pncommerce', 'user', 'readfromDB',
                                                           array( 'table' => 'category',
                                                                  'fields' => array( 'Category', 'CatParent', 'VATID' ),
                                                                  'required' => array( 'CatInvisible', '', 'CategoryID', $CategoryID ) ) );
            $cat['Name'] = $CategoryName;
            $cat['ID'] = $CategoryID;
            $cat['parentID'] = $ParentID;
            $cat['VATID'] = $VATID;
            $cat['LinkURL'] = pnModAPIFunc( 'pncommerce', 'user', 'pncModURL',
                                     array( 'Module' => 'pncommerce',
                                            'Section' => 'user',
                                            'Function' => 'categoryview',
                                            'Args' => array( 'CID' => $CategoryID ) ) );
            // add the new category as first in the array
            array_unshift( $catlist, $cat );
            $CategoryID = $ParentID;
        }
        $smarty->assign( $assign, $catlist );
    }
    else
    {
        $smarty->trigger_error( "pncGetCategoryPathList: cannot load pnuserapi", E_USER_ERROR );
        return false;
    }
}

?>
